// pages/my/my.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoggedIn: false, // 用户登录状态
    show:true,
    username:'',
  myUsername:"fgdfred",
  maskedPhoneNumber:'',
   phoneNumber :'',
   bindusername:"",
   stringusername:'',
  num:1,
  page:8,
  },
  phone: function(){
    let that = this
    console.log("121")
    //先调用后端接口找到username的信息，找到对应的手机号，然后加密完成后，传给下一个页面
    wx.request({
      url: 'http://localhost:8080/user/selectNameOrNumber',
      method:'GET',
      data:{
        username:this.data.username
      },
      success(res){
        if(res.data){
          console.log(res.data.phone);
          console.log(res.data.username);
          that.data.bindusername = res.data.username;
          console.log(that.data.bindusername);
          console.log(11111);
          wx.navigateTo({            
            url: `/pages/phone/changephone/changephone?username=${that.data.bindusername}`,
          })
          const maskPhoneNumber = () => {
            let phoneNumber = res.data.phone;
            console.log(res.data.phone);
            // 确保 phoneNumber 是字符串类型
            if (typeof phoneNumber !== 'string') {
              phoneNumber = phoneNumber.toString();  // 转换为字符串
            }
    
            // 将电话号码的第4到第7位替换为 "*"
            let maskedPhoneNumber = phoneNumber.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
    
            // 跳转到下一个页面并传递处理后的电话号码
            wx.navigateTo({
              url: `/pages/phone/bindphone/bindphone?phoneNumber=${maskedPhoneNumber}`,
            });
          }
          // 调用函数
      maskPhoneNumber();
        }else{
          console.log("错误");
        }
      },
    })
    //将id存入缓存中
    //或者传到下一个页面或加密展示
    
    console.log("323")
  },
  updatePwd:function(){
    console.log("121")
    wx.navigateTo({
 
      url: '/pages/updatePwd/updatePwd',
    }),
    console.log("323")
  },
  //退出登录
  toPageLogin(){
    app.globalData.username=null,
    console.log(app.globalData.username);
    this.data.show=true,
      wx.redirectTo({
      url: '/pages/login/login',
    })

  },
  //关于我们
  toAboutPage(){
    wx.navigateTo({
        url: '/pages/about/about',
    })
  },
 toMyTest(){
    let that=this;
    wx.request({
      url: app.globalData.baseUrl+'/heartmark/selectAll',
      data:{
        pageNum:this.data.num,
        pageSize:this.data.page
      },
      method:'GET',
      // 请求成功时的数据
      success(res){
        console.log(res.data.data);
        if(res.data.data && res.data.data.length > 0){
          // 数据存在
          wx.navigateTo({
            url: '/pages/myDetail/myTest/myTest',
          });
        }else{
          wx.navigateTo({
            url: '/pages/myDetail/myNoTest/myNoTest',
          });
        }
      },
     // 请求失败处理
      fail() {
        wx.showToast({
          title: '请求失败，请重试',
          icon: 'none'
        });
      }
    })
  },
  // 登录 / 注册 跳转
  LoginOrRegister(){
    wx.navigateTo({
      url: '/pages/login/login',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  
  },

  logout() {
    // 用户退出登录时调用
    wx.removeStorageSync('username'); // 清除用户信息
    this.setData({
      isLoggedIn: false,
      username: ''
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const username = app.globalData.username;
    console.log(app.globalData.username);
    console.log(username);
    this.data.username = username;
    console.log(username);
    console.log(6666);
  
    if(app.globalData.username == null){
      console.log(123);
      console.log(this.data.show);
    }else{
      this.setData({
        username: username,
        show:false
      });
      
    }
    let that = this;
    //.toString()
    this.data.stringusername = this.data.username.toString();
    wx.request({
      url: 'http://localhost:8080/user/selectNameOrNumber',
      method:'GET',
      data:{
        username:this.data.stringusername
      },
      success(resp){
        console.log(resp.data.phone);
        that.data.username= resp.data.phone,
        console.log(that.data.username);
        console.log(5555);
      }
    })
    
    

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})